'use strict';

import Base from './base.js';

export default class extends Base {

    async indexAction() {
        let data = await this.model("s_user").page(1, 10).countSelect();
        this.success(data);
    }

    async roleAction() {
        let name = this.post("name");
        let roles = this.post("roles");

        let datas = [];
        let roleIds = await this.model('s_role').where({name: ['IN', roles]}).select();
        if (!think.isEmpty(roleIds)) {
            for (let i = 0; i < roleIds.length; i++) {
                datas.push({userId: name, roleId: roleIds[i].id});
            }
            if(datas.length>0){
                // 删除历史角色
                await this.model('s_user_role').where({userId: name}).delete();
                // 保存新角色
                let result = await this.model('s_user_role').addMany(datas);
                console.info(result);
                this.success(result);
            }else{
                this.fail('保存失败!');
            }
        }
    }
}